package com.work.servlet;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.work.dao.CarDao;
import com.work.dao.CarDaoImpl;
import com.work.pojo.Car;
import com.work.pojo.CarInfo;
import com.work.pojo.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.*;

/**
 * @ClassName: ${NAME}
 * @Description: TODO
 * @author: meyolo
 * @date: 2021/3/22  21:41
 * 购物车操作
 */
@WebServlet(name = "CarServlet", urlPatterns = {"/pushcar", "/showcar", "/clearcar", "/deletegood", "/update","/buy"})
public class CarServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String Path = request.getServletPath();

        switch (Path) {
            case "/pushcar":
                pushCar(request, response);
                break;
            case "/showcar":
                showcar(request, response);
                break;
            case "/clearcar":
                clearcar(request, response);
                break;
            case "/deletegood":
                deletegood(request, response);
            case "/update":
                updatecar(request, response);
            case "/buy":
                buy(request,response);
                break;
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    //加入购物车
    private void pushCar(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        User user = (User) request.getSession().getAttribute("state");
        String goodId = request.getParameter("goodId");
        String mynum = request.getParameter("num");
        if (mynum == null) {
            mynum = "1";
        }
        int id = Integer.parseInt(goodId);
        int num = Integer.parseInt(mynum);
        int user_id = user.getUser_id();
        CarDao carDao = new CarDaoImpl();
        Car car = carDao.IsRepeatGoods(id, user_id);
        if (car != null) {
            Car car1 = carDao.GoodsNum(user_id, id);
            carDao.insertRepeatGoodsCar(user_id, id, car1.getGoods_num() + num);
        } else {
            carDao.insertNewGoodsCar(num, id, user_id);
        }
        response.sendRedirect("showcar");
    }

    //显示购物车
    private void showcar(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        User user = (User) request.getSession().getAttribute("user");
        CarDao carDao = new CarDaoImpl();
        List<CarInfo> carInfos = carDao.queryCarInfo(user.getUser_id());

        float allprice = 0;
//        计算购物车总价格
        for (CarInfo car : carInfos) {
            allprice += car.getGoods_num() * car.getGoods_price();
        }

        request.setAttribute("carInfos", carInfos);
        request.setAttribute("allprice", allprice);
        request.getRequestDispatcher("/car.jsp").forward(request, response);
    }

    //清空购物车
    private void clearcar(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        User user = (User) request.getSession().getAttribute("user");
        CarDao carDao = new CarDaoImpl();
        carDao.clearCar(user.getUser_id());
        request.getRequestDispatcher("/showcar").forward(request, response);
    }



    //购买
    private void buy(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        User user = (User) request.getSession().getAttribute("user");
        String path = request.getParameter("path");
        if(user==null){
            response.getWriter().write("<script>alert('请您先登录');location.href='/login.jsp';</script>");
        }else if(path==null){
            clearcar(request,response);
            response.getWriter().write("<script>alert('购买成功');location.href='/showcar';</script>");
        }else{
            response.getWriter().write("<script>alert('购买成功');location.href='/query';</script>");
        }

    }

    //通过编号删除购物车中的商品
    private void deletegood(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取user_id
        User user = (User) request.getSession().getAttribute("user");
        //获取货物编号
        String goods_id = request.getParameter("goods_id");
        CarDao carDao = new CarDaoImpl();
        carDao.deleteGoodsInCarByid(user.getUser_id(), Integer.parseInt(goods_id));
        request.getRequestDispatcher("/showcar").forward(request, response);
    }

    private void updatecar(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取user_id
        User user = (User) request.getSession().getAttribute("user");
        int user_id = user.getUser_id();
        //获取货物编号
        int id = Integer.parseInt(request.getParameter("goods_id"));
//           货品数量
        int num = Integer.parseInt(request.getParameter("num"));
        CarDao carDao = new CarDaoImpl();
//           更新货品数量

        int i = carDao.insertRepeatGoodsCar(user_id, id, num);
        //计算总价格
        List<CarInfo> carInfos = carDao.queryCarInfo(user.getUser_id());
        float allprice = 0;
//        计算购物车总价格
        for (CarInfo car : carInfos) {
            allprice += car.getGoods_num() * car.getGoods_price();
        }
        System.out.println(allprice);
        if (i > 0) {
//                使用 jackson
            ObjectMapper objectMapper = new ObjectMapper();
            String info = objectMapper.writeValueAsString(allprice);
            //向AJAX发送信息
            response.setContentType("text/html;charSet=utf-8");
            PrintWriter writer = response.getWriter();
            writer.write(info);
            writer.flush();
            writer.close();
        }

    }
}
